#!/usr/bin/perl
#
# Copyright (c) 2013-2014 Luciano Xumerle. All rights reserved.
#
# This file is part of gephinfo.
#
# gephinfo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use warnings;

my @WRAPPED =
  ( 'ConvertGenotype', 'JenoConverter', 'JenoHWE', 'JoinCSV', 'JoinTables' );

if ( !defined $ARGV[0] )
{
    &help();
}
elsif ( $ARGV[0] eq 'all' )
{
    &clean();
    &getJenowareJar();
    &install();
    &javaWrapper();
    &clean();
}
elsif ( $ARGV[0] eq 'make' )
{
    &clean();
    &getJenowareJar();
}
elsif ( $ARGV[0] eq 'clean' )
{
    &clean();
}
elsif ( $ARGV[0] eq 'install' )
{
    &install();
    &javaWrapper();
    &clean();
}
else { &help(); }

#
# PRINT HELP
#
sub help
{
    print STDERR qq|
    all : do all: make, install, clean
   make : build all
install : install in prefix (/usr/local)
  clean : clean all
\n|;
}

#
# COMPILE JENOWARE AND MAKEJAR FILE
#
sub getJenowareJar
{
    mkdir 'build';
    system( '/usr/bin/javac', '-cp', 'lib/Jenoware', '-d', 'build', '-source',
        '1.4', '-target', '1.4', @{ &getJavaFile() } );
    chdir 'build';
    system( '/usr/bin/jar', 'cvf', '../jenoware.jar', '.' );
    chdir '..';
}

#
# RETURN LIST OF JAVA FILE
#
sub getJavaFile
{
    my @a = `/usr/bin/find lib/Jenoware -type f -iname '*.java'`;
    foreach (@a) { chomp; }
    return \@a;
}

#
# CLEAN TEMPORARY FILES
#
sub clean
{
    system( '/bin/rm', '-fr', 'build/', 'jenoware.jar' );
}

#
# INSTALL
#
sub install
{
    mkdir '/usr/local/share/gepinfo' if ( !-d '/usr/local/share/gepinfo' );
    system( '/usr/bin/install', '-m', 644, 'jenoware.jar', 'COPYING', 'README',
        '/usr/local/share/gepinfo' );
    foreach my $src ( @{ &getEXE() } )
    {
        system( '/usr/bin/install', '-m', 755, $src, '/usr/local/bin' );
    }

    # perl lib
    mkdir('/usr/local/lib/site_perl') if ( !-d '/usr/local/lib/site_perl' );
    foreach my $lib ( glob("lib/perl/*pm") )
    {
        system( '/usr/bin/install', '-m', '644', $lib,
            '/usr/local/lib/site_perl' );
    }
}

#
# USED TO GET THE LIST OF FILES IN DIRECTORY:
# gephinfo/bin
#
sub getEXE
{
    my @a = `/usr/bin/find bin -type f`;
    foreach (@a) { chomp; }
    return \@a;
}

#
# CREATE AND INSTALL WRAPPER TO JENOWARE PROGRAMS
#
sub javaWrapper
{
    foreach my $class (@WRAPPED)
    {
        my $dest = '/usr/local/bin/' . lcfirst($class);
        open( FILE, ">$dest" ) || die;
        print FILE qq|#!/bin/bash\n# gephinfo wrapper\n|;
        print FILE q|/usr/bin/java -cp /usr/local/share/gepinfo/jenoware.jar |,
          $class, q| $@|, "\n";
        close FILE;
        system( '/bin/chmod', 755, $dest );
    }
}
